import { defineStore } from "pinia"
import {
  GetInit,
  GetOptionPerm,
  GetOptionMenu,
  GetOptionRoles,
  GetOptionRoleTree,
  GetOptionRole
} from "@/service/api"
import type { mainState } from "../type"
import { setDynamicRoute } from "@/utils/setDynamicRoute"

export const mainStore = defineStore({
  //js命名
  id: "main",
  // 数据
  state: (): mainState => {
    return {
      // 菜单权限
      menuList: [],
      // 按钮权限
      perm: [],
      // 所属菜单（菜单管理）
      menuMenuOptions: [],
      // 所属菜单（权限管理）
      permMenuOptions: [],
      // 所有角色
      rolesOptions: [],
      // 权限树
      permTree: [],
      // 角色权限
      rolePerm: []
    }
  },
  // computed 修饰一些值
  getters: {},
  // methods, 同步和异步都可以, 修改state
  actions: {
    // 初始化菜单和按钮权限
    async getInit() {
      const res: any = await GetInit()
      // console.log(res.data.menu)
      this.menuList = res.data.menu
      this.perm = res.data.perm
      // 设置动态路由
      setDynamicRoute(this.menuList)
    },
    // 获取所属菜单（菜单管理）
    async getOptionMenu() {
      const res: any = await GetOptionMenu()
      this.menuMenuOptions = res.data
    },
    // 获取所属菜单（权限管理）
    async getOptionPerm() {
      const res: any = await GetOptionPerm()
      this.permMenuOptions = res.data
    },
    // 获取所有角色
    async getOptionRoles() {
      const res: any = await GetOptionRoles()
      this.rolesOptions = res.data
    },
    // 获取权限树
    async getOptionRoleTree() {
      const res: any = await GetOptionRoleTree()
      this.permTree = res.data
    },
    // 获取角色权限
    async getOptionRole(id: number) {
      const res: any = await GetOptionRole(id)
      this.rolePerm = res.data
      console.log(this.rolePerm)
    }
  }
})
